package com.aplink.generic.widgets;

import android.content.Context;
import android.content.res.Resources;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.util.AttributeSet;
import android.util.DisplayMetrics;
import android.view.View;

import com.aplink.sg.R;

public class ApLinkWaitingBar extends View {
	private static final float PROGRESS_BAR_HEIGHT = 4;
	private SwipeProgressBar mProgressBar;
	private int mProgressBarHeight;

	public ApLinkWaitingBar(final Context context) {
		super(context);
	}

	public ApLinkWaitingBar(final Context context, final AttributeSet attrs) {
		super(context, attrs);
		setWillNotDraw(false);
		mProgressBar = new SwipeProgressBar(this);
		final DisplayMetrics metrics = getResources().getDisplayMetrics();
		mProgressBarHeight = (int) (metrics.density * PROGRESS_BAR_HEIGHT);
		final TypedArray a = context.getTheme().obtainStyledAttributes(attrs,
		        R.styleable.ApLinkWaitingBar, 0, 0);
		setColorScheme(a);
		start();
	}

	@Override
	public void draw(final Canvas canvas) {
		super.draw(canvas);
		mProgressBar.draw(canvas);
	}

	@Override
	protected void onLayout(final boolean changed, final int left,
	        final int top, final int right, final int bottom) {
		mProgressBar.setBounds(0, 0, getWidth(), mProgressBarHeight);
	}

	public void setColorScheme(final TypedArray a) {
		final Resources res = getResources();
		final int color1 = res.getColor(android.R.color.holo_blue_bright);
		final int color2 = res.getColor(android.R.color.holo_green_light);
		final int color3 = res.getColor(android.R.color.holo_orange_light);
		final int color4 = res.getColor(android.R.color.holo_red_light);
		mProgressBar.setColorScheme(
		        a.getColor(R.styleable.ApLinkWaitingBar_ColorScheme_1, color1),
		        a.getColor(R.styleable.ApLinkWaitingBar_ColorScheme_2, color2),
		        a.getColor(R.styleable.ApLinkWaitingBar_ColorScheme_3, color3),
		        a.getColor(R.styleable.ApLinkWaitingBar_ColorScheme_4, color4));
	}

	public void start() {
		mProgressBar.start();
	}

	public void stop() {
		mProgressBar.stop();
	}

}
